"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : Compatibility
Case Name   : col_name_or_user_var指定列字段不为空,部分测试点合理报错
Create At   : 2023/06/02
Owner       : n@ningyali
Description :
        1.创建表
        2.col_name_or_user_var指定任意一列,非主键列
        3.col_name_or_user_var指定任意一列，主键列
        4.col_name_or_user_var指定多列
        5.col_name_or_user_var指定重复列
        6.col_name_or_user_var按照列名顺序导入
        7.col_name_or_user_var不按照列名顺序导入
Expect      :
        1.成功
        2.合理报错
        3.成功
        4.成功
        5.合理报错
        6.成功
        7.成功
History     :
"""
import os
import unittest

from yat.test import Node

from testcase.COMPATIBILITY.GRAMMAR.LOAD. \
    Opengauss_Compatibility_Grammar_Load_Common import LoadData
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class LoadData0057(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.load_data = LoadData()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.case_lable = 'load_data_0057'
        self.db_name = f'db_{self.case_lable}'
        self.t_name = f't_{self.case_lable}'
        self.file_path = os.path.join('/home', self.pri_user.ssh_user)
        self.cols = 'c_id int primary key,c_name varchar(36),' \
                    'c_salary decimal(9,2),c_address varchar(255),' \
                    'c_date datetime'

        text1 = '-----创建数据文件；expect:创建成功-----'
        self.log.info(text1)
        self.data1 = "#3;#4;#5;"
        self.data2 = "#3|张三|北京;#4|李四|广州;#5|王五|西安;"
        self.data3 = "#3|3|1000.00|1000.00|北京;" \
                     "#4|4|2000.00|2000.00|广州;" \
                     "#5|5|1800.88|1800.88|西安;"
        self.data4 = "#3|张三|2200.888|北京|2022-09-19 00:00:00;" \
                     "#4|李明|3200.359|xian|2022-05-12 00:00:00;" \
                     "#5|孙权|1200.9999|广州|2022-12-12 12:12:12;"
        self.data5 = "#3|张三|2022-09-19 00:00:00|北京|2200.888;" \
                     "#4|李明|2022-05-12 00:00:00|xian|3200.359;" \
                     "#5|孙权|2022-12-12 12:12:12|广州|1200.9999;"
        res = self.load_data.create_data_file(self.pri_user, self.case_lable,
                                              self.data1, self.data2,
                                              self.data3, self.data4,
                                              self.data5)
        self.assertTrue(res, '执行失败:' + text1)

        text2 = '-----创建兼容B库；expect:创建成功-----'
        self.log.info(text2)
        res = self.load_data.create_b_db(self.pri_sh, self.db_name)
        self.assertTrue(res, '执行失败:' + text2)

    def test_load(self):
        text = '-----step1：创建表; expect：建表成功-----'
        self.log.info(text)
        res = self.load_data.create_table(self.pri_sh, self.db_name,
                                          self.t_name, self.cols)
        self.assertTrue(res, '执行失败:' + text)

        text = '-----step2：col_name_or_user_var指定任意一列,非主键列; expect:合理报错-----'
        self.log.info(text)
        f_path1 = os.path.join(self.file_path, f'{self.case_lable}_1.txt')
        res1 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path1,
                                        encoding='utf8', into_type='',
                                        col_sep='|', cols_name='c_address')
        err_info = 'ERROR:  null value in column "c_id" violates ' \
                   'not-null constraint'
        self.assertIn(err_info, res1, '执行失败' + text)

        text = '-----step3：col_name_or_user_var指定任意一列，主键列; expect:成功-----'
        self.log.info(text)
        res2 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path1,
                                        encoding='utf8', into_type='',
                                        col_sep='|', cols_name='c_id')
        rows, _ = self.common.parse_sql_query_res(res2)
        expect = [['3', '', '', '', ''], ['4', '', '', '', ''],
                  ['5', '', '', '', '']]
        self.assertEqual(rows, expect, '执行失败' + text)

        text = '-----step4：col_name_or_user_var指定多列; expect:成功-----'
        self.log.info(text)
        f_path2 = os.path.join(self.file_path, f'{self.case_lable}_2.txt')
        res3 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path2,
                                        encoding='utf8', into_type='',
                                        col_sep='|',
                                        cols_name='c_id,c_name,c_address')
        rows, _ = self.common.parse_sql_query_res(res3)
        expect = [['3', '张三', '', '北京', ''],
                  ['4', '李四', '', '广州', ''],
                  ['5', '王五', '', '西安', '']]
        self.assertEqual(rows, expect, '执行失败' + text)

        text = '-----step5：col_name_or_user_var指定重复列; expect:合理报错-----'
        self.log.info(text)
        f_path3 = os.path.join(self.file_path, f'{self.case_lable}_3.txt')
        res4 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path3,
                                        encoding='utf8', into_type='',
                                        col_sep='|',
                                        cols_name='c_id,c_id,c_salary,'
                                                  'c_salary,c_address')
        err_info = 'ERROR:  column "c_id" specified more than once'
        self.assertIn(err_info, res4, '执行失败' + text)

        text = '-----step6：col_name_or_user_var按照列名顺序导入; expect:成功-----'
        self.log.info(text)
        f_path4 = os.path.join(self.file_path, f'{self.case_lable}_4.txt')
        res5 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path4,
                                        encoding='utf8', into_type='',
                                        col_sep='|',
                                        cols_name='c_id,c_name,c_salary,'
                                                  'c_address,c_date')
        rows, _ = self.common.parse_sql_query_res(res5)
        expect = [['3', '张三', '2200.89', '北京', '2022-09-19 00:00:00'],
                  ['4', '李明', '3200.36', 'xian', '2022-05-12 00:00:00'],
                  ['5', '孙权', '1201.00', '广州', '2022-12-12 12:12:12']]
        self.assertEqual(rows, expect, '执行失败' + text)

        text = '-----step7：col_name_or_user_var不按照列名顺序导入; expect:成功-----'
        self.log.info(text)
        f_path5 = os.path.join(self.file_path, f'{self.case_lable}_5.txt')
        res6 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path5,
                                        encoding='utf8', into_type='',
                                        col_sep='|',
                                        cols_name='c_id,c_name,c_date,'
                                                  'c_address,c_salary')
        rows, _ = self.common.parse_sql_query_res(res6)
        self.assertEqual(rows, expect, '执行失败' + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        res = self.load_data.clear_env(self.pri_sh, self.pri_user,
                                       self.db_name,
                                       os.path.join(self.file_path,
                                                    f"{self.case_lable}*"))
        self.assertTrue(res, '执行失败:清理环境')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
